<?php


namespace app\api\controller\v20;


use app\api\controller\v2\Auth;
use app\common\cachemodel\ZlFortuneUp;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use app\common\model\FortuneCache;
use app\common\model\Recode;

class Fortune extends Auth
{
    protected $active_switch;

    /**
     * 噩运消除宜忌文案
     * @var \string[][]
     */
    protected $fortuneUpText = [
        '婚恋' => [
            'fortune_up_tip' => '金星-婚恋',
            'lucky_text' => ['感情甜蜜', '浓情蜜意'],
            'avoid_text' => ['情路坎坷', '情路不顺'],
            'pid' => 3,
            'lucky_tip' => '金星复位，情投意合'
        ],
        '学业' => [
            'fortune_up_tip' => '水星-学业',
            'lucky_text' => ['条理清晰', '头脑清晰'],
            'avoid_text' => ['头脑迷糊', '思维涣散'],
            'pid' => 2,
            'lucky_tip' => '水星复位，才思敏捷'
        ],
        '工作' => [
            'fortune_up_tip' => '太阳-工作',
            'lucky_text' => ['职场得意', '事业兴旺'],
            'avoid_text' => ['职场失意', '工作坎坷'],
            'pid' => 0,
            'lucky_tip' => '太阳复位，前程似锦'
        ],
        '财富' => [
            'fortune_up_tip' => '火星-财富',
            'lucky_text' => ['财源滚滚', '财源广进'],
            'avoid_text' => ['冲动消费', '破财免灾'],
            'pid' => 4,
            'lucky_tip' => '火星复位，财运亨通'
        ],
        '出行' => [
            'fortune_up_tip' => '木星-出行',
            'lucky_text' => ['一路顺风', '旅途愉快'],
            'avoid_text' => ['出行遇阻', '计划生变'],
            'pid' => 5,
            'lucky_tip' => '木星复位，精力充沛'
        ],
        '健康' => [
            'fortune_up_tip' => '土星-健康',
            'lucky_text' => ['元气满满', '生机勃勃'],
            'avoid_text' => ['注意健康', '状态低迷'],
            'pid' => 6,
            'lucky_tip' => '土星复位，长乐安康'
        ],
        '交际' => [
            'fortune_up_tip' => '天王星-交际',
            'lucky_text' => ['左右逢源', '人缘鼎盛'],
            'avoid_text' => ['人际紧张', '性格孤僻'],
            'pid' => 7,
            'lucky_tip' => '天王星复位，人际开花'
        ],
        '家人' => [
            'fortune_up_tip' => '月亮-家人',
            'lucky_text' => ['天伦之乐', '美满团圆'],
            'avoid_text' => ['家庭矛盾', '亲情冷淡'],
            'pid' => 1,
            'lucky_tip' => '月亮复位，其乐融融'
        ],
        '心情' => [
            'fortune_up_tip' => '海王星-心情',
            'lucky_text' => ['乐观向上', '身心愉悦'],
            'avoid_text' => ['emo至极', '迷茫无助'],
            'pid' => 8,
            'lucky_tip' => '海王星复位，心旷神怡'
        ],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->active_switch = false;
    }

    /**
     * @title 每日运势详情
     * @method GET
     * @desc
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     */
    public function index()
    {
        $rid = input('get.rid/d', 0);
        // 当前时间
        $time = strtotime(input('get.time/s'));

        if (!$rid)
        {
            return show(config('code.error'), '档案信息获取失败');
        }
        $uid = (int)$this->user['id'];

        $recode = (new Recode())->getRecode(['id' => $rid]);
        if (!$recode || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }

        //获取日运详情数据
        $data = (new FortuneCache())->getIndexInfo($uid, $time, $rid, $this->active_switch, 2);

        if (isset($data['fortune_list']))
        {   // 已解锁
            $data = $this->fortuneDataReset($data, $rid, (int)$recode['isself'], $time);
        }

        return show(200, '加载成功', $data);
    }

    /**
     * 运势信息新增厄运消除数据结构
     * @param array $data
     * @param int $rid
     * @param int $isSelf
     * @param int $time
     * @return array
     * @throws \Exception
     */
    private function fortuneDataReset(array $data, int $rid, int $isSelf, int $time): array
    {
        // $colors = [
        //     0 => ['name' => '困难', 'color' => '#7775ff',],
        //     1 => ['name' => '警戒', 'color' => '#ffa536',],
        //     2 => ['name' => '平平', 'color' => '#ffed17',],
        //     3 => ['name' => '吉利', 'color' => '#00f4e7',],
        //     4 => ['name' => '大吉', 'color' => '#00e9a0',],
        // ];
        foreach ($data['fortune_list'] as $index => $fortuneItem)
        {
            $data['fortune_list'][$index]['is_bad_luck'] = in_array($fortuneItem['color'], ['#ffed17', '#ffa536', '#7775ff'], true) ? 1 : 0;
            $data['fortune_list'][$index]['fortune_up_tip'] = $this->fortuneUpText[$fortuneItem['name']]['fortune_up_tip'];
            $data['fortune_list'][$index]['pid'] = $this->fortuneUpText[$fortuneItem['name']]['pid'];
            $data['fortune_list'][$index]['lucky_tip'] = $this->fortuneUpText[$fortuneItem['name']]['lucky_tip'];
        }

        // v2.7.2 新增 **之日 分享的图片地址
        $iconPath = Util::CheckAppVersion('2.4.2') === '小' ? 'v2' : 'v3';
        foreach ($data['revive_day'] as $index => $reviveDayItem)
        {
            $new_icon = explode('/', $reviveDayItem['icon_url']);

            array_splice($new_icon, 5, 0, [$iconPath]);

            $data['revive_day'][$index]['icon2_url'] = implode('/', $new_icon);
        }

        if ($isSelf && (($timeDate = date('Ymd', $time)) === date('Ymd')))
        {
            $timeDate = (int)$timeDate;
            // 获取运势提升状态 1已提升 0未提升
            $isFortuneUp = (new ZlFortuneUp())->getFortuneUpStatus($rid, $timeDate);
            $data['fortune_up_btn_show'] = 0; //默认不提示

            $textList = [];
            $rd = date('d') % 2;
            foreach ($data['fortune_list'] as $fortuneItem)
            {
                if (!$fortuneItem['is_bad_luck'])
                {
                    $textList[] = [
                        'text' => $this->fortuneUpText[$fortuneItem['name']]['lucky_text'][$rd],
                        'color' => '#FFFFFF'
                    ];
                    continue;
                }

                if ($isFortuneUp)
                {   // 已提升运势
                    $textList[] = [
                        'text' => $this->fortuneUpText[$fortuneItem['name']]['lucky_text'][$rd],
                        'color' => '#00f4e7'
                    ];
                } else
                {
                    $textList[] = [
                        'text' => $this->fortuneUpText[$fortuneItem['name']]['avoid_text'][$rd],
                        'color' => $fortuneItem['color']
                    ];
                }

                $data['fortune_up_btn_show'] = 1; // 有厄运情况 提示提升按钮
            }

            if ($data['fortune_up_btn_show'])
            {
                $data['fortune_up_btn_show'] = $isFortuneUp ? 0 : 1; // 有厄运情况 提示提升按钮
                $data['fortune_up_info'] = [
                    'title' => $isFortuneUp ? '今日运势状况：' : '当前运势状态',
                    'desc' => $isFortuneUp ? '下划线词条对应已提升运势，颜色代表该运势状态。' : '下划线词条对应可提升运势，颜色代表该运势状态。',
                    'text_list' => $textList
                ];
            } else
            {
                $data['fortune_up_info'] = [
                    'title' => $isFortuneUp ? '今日运势状况：' : '当前运势状态',
                    'desc' => $isFortuneUp ? '下划线词条对应已提升运势，颜色代表该运势状态。' : '下划线词条对应可提升运势，颜色代表该运势状态。',
                    'text_list' => $textList
                ];
                $data['fortune_up_btn_show'] = 0;
            }

            $data['poster_data'] = [
                'title' => 'DIY专属海报',
                'desc' => '去分享自己亲手设计的准了海报吧！～',
                'is_new' => 1,
            ];
        } else
        {
            $data['fortune_up_btn_show'] = 0;
            $data['fortune_up_info'] = null;
        }

        return $data;
    }

    /**
     * 运势提升
     */
    public function fortuneUp()
    {
        $rid = input('rid/d', 0);
        if (!$rid)
        {
            return show(config('code.error'), '请求异常');
        }

        $recode = (new Recode())->getRecode(['id' => $rid]);
        if (!$recode || $recode['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }

        $data = [
            'uid' => (int)$this->user['id'],
            'rid' => $rid,
            'time' => (int)(date('Ymd')),
            'create_time' => $this->request->time()
        ];

        (new ZlFortuneUp())->recodeFortuneUp($data);

        return show(200, '运势提升成功');
    }
}
